package com.stdspringboot.ctrl;

import com.stdspringboot.entity.User;
import com.stdspringboot.service.IRegService;
import com.stdspringboot.vo.JsonResult;
import com.stdspringboot.vo.UserVO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

//该注解表示 mvc 的controller 层，加上该注解后，才能接收浏览器发来的请求
//@Controller
@RestController
public class RegCtrl {

    @Resource
    private IRegService service;

    @RequestMapping("/reg")
//    @ResponseBody
    public JsonResult reg(UserVO user){
        //非空校验
        if(user == null){
//            JsonResult jr=new JsonResult(JsonResult.CODE_VALIDATE_FAIL,"请传递数据！");
//            return jr;
            return JsonResult.validateFail("请输入数据！");
        }
        if(user.getUsername() == null || "".equals(user.getUsername())){
//            JsonResult jr=new JsonResult(JsonResult.CODE_VALIDATE_FAIL,"请输出登录名！");
//            return jr;
            return JsonResult.validateFail("请输出登录名！");
        }
        String password=user.getPassword(); // 密码
        String password2=user.getPassword2();//确认密码
        if(password==null || "".equals(password)){
            return JsonResult.validateFail("请输出密码！");
        }
        if(password2==null || "".equals(password2)){
            return JsonResult.validateFail("请输出确认密码！");
        }
        if( ! password.equals(password2)){
            //因为 校验2次密码是否相同，应用场景非常少，就没有必要封装 成独立的方法
            JsonResult jr=new JsonResult(JsonResult.CODE_PASSWORD_NOT_EQUAL,"密码与确认密码不相同，请重新填写！");
            return jr;
        }

        JsonResult jr=null;
        try {
            jr=service.reg(user);
        } catch (Exception e) {
            e.printStackTrace();
            jr=JsonResult.serverError();
        }

        return jr;
    }
}
